package com.linzhaowei.headhuntingservice;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.linzhaowei.headhuntingservice.adapter.JobObjectiveSimpleAdapter;
import com.linzhaowei.headhuntingservice.bean.JobObjective;
import com.linzhaowei.headhuntingservice.utils.HttpUtils;
import com.linzhaowei.headhuntingservice.utils.Ip;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class JobObjectiveManage extends AppCompatActivity implements View.OnClickListener{
    private RecyclerView recyclerView;
    private Button button;

    JobObjectiveSimpleAdapter jobObjectiveSimpleAdapter;

    List<JobObjective> jobObjectiveList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_job_objective_manage);
        initViews();
        setEvents();
    }

    @Override
    protected void onStart() {
        super.onStart();
        getJobObjective();
    }


    private void initViews(){
        recyclerView=findViewById(R.id.job_objective_manage);
        button=findViewById(R.id.j_o_m_btn);
    }

    private void setEvents(){
        button.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.j_o_m_btn:
                Intent intent=new Intent(JobObjectiveManage.this,JobObjectiveAdd.class);
                startActivity(intent);
                break;
                default:
                    break;
        }
    }

    private void getJobObjective(){
        new Thread(()->{
            HttpUtils httpUtils=new HttpUtils();
            try{
                SharedPreferences sharedPreferences= getSharedPreferences("user", Context.MODE_PRIVATE);
                String result=httpUtils.get(Ip.ip+"/user/job_objective_by_user?user_id="+ sharedPreferences.getInt("id", -1));
                JSONArray jsonArray= JSON.parseArray(result);
                jobObjectiveList=new ArrayList<>();
                for(int i=0;i<jsonArray.size();i++){
                    jobObjectiveList.add(jsonArray
                    .getJSONObject(i)
                    .toJavaObject(JobObjective.class));
                }

                show();

            }catch (IOException e){
                e.printStackTrace();
            }
        }).start();
    }

    void show(){
        runOnUiThread(()->{
            LinearLayoutManager linearLayoutManager=new LinearLayoutManager(this);
            recyclerView.setLayoutManager(linearLayoutManager);
            jobObjectiveSimpleAdapter=new JobObjectiveSimpleAdapter(jobObjectiveList,this);
            recyclerView.setAdapter(jobObjectiveSimpleAdapter);
        });

    }


}
